import pymysql

from . import config


class DataBase:

    def establish_database_connection(self):

        """ Устанавливаем соединение с базой данных """

        try:

            connection = pymysql.connect(host='localhost',
                                         user=config.user,
                                         password=config.password,
                                         database='fl',
                                         cursorclass=pymysql.cursors.DictCursor
                                         )

            return connection

        except Exception as e:

            print('Что-то не то')
            print(e)


    def get_existing_records(self):

        """ Забираем имеющиеся записи """

        connection = self.establish_database_connection()

        with connection.cursor() as cursor:

            get_all_records_sql = "SELECT * FROM OrderLink"
            cursor.execute(get_all_records_sql)
            all_records = cursor.fetchall()

        connection.close()

        return all_records


    def add_new_entry(self, entry):

        """ Добавляем ссылки новых заказов в базу """

        connection = self.establish_database_connection()

        with connection.cursor() as cursor:

            add_new_entry_sql = "INSERT INTO OrderLink(link) VALUES(%s)"
            cursor.execute(add_new_entry_sql, (entry,))

        connection.commit()
        connection.close()
